package com.mdp.im.core.publish.action.impl.msgaction;

import com.alibaba.fastjson.JSONObject;
import com.mdp.core.api.Sequence;
import com.mdp.im.core.entity.ImPrichatMessage;
import com.mdp.im.core.entity.ImUser;
import com.mdp.im.core.publish.api.Publisher;
import com.mdp.im.core.publish.api.action.PrichatMsgAction;
import com.mdp.im.core.service.ImPrichatMessageService;
import com.mdp.im.core.service.ImUserService;
import com.mdp.im.core.utils.msg.MsgAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
/**
 * 交换双方通讯方式
 * msgId:xxxx,
 * groupId:xxxxx,
 * msgType:prichat,
 * action:newMessage,
 * sendUserid:xxx,
 * toUserid:xxxxx,
 * sourceMsgId:xxxxx,///agreeJoinFrend/refuseJoinFrend/时需要
 * msgAction: inviteJoinFrend/agreeJoinFrend/refuseJoinFrend/delJoinFrend/blackJoinFend/removeBlackFrend 邀请/同意/拒绝/删除/拉黑/拉出黑名单
 * 
 * @author qqkj
 *
 */
public class ExchangeContactMsgAction implements PrichatMsgAction {

	
	@Autowired
	ImPrichatMessageService prichatMessageService;

	@Autowired
	Publisher publisher;

	@Autowired
	ImUserService imUserService;

	@Autowired
	Sequence seqService;
	
	@Override
	public void doAction(JSONObject message) {
		String msgAction=message.getString("msgAction");
		String sendUserid=message.getString("sendUserid");
		String sendUsername=message.getString("sendUsername");
		String toUserid=message.getString("toUserid");
		String toUsername=message.getString("toUsername");
		if(MsgAction.prichat_agreeExchangeContact.equals(msgAction)) {
			String sourceMsgId=message.getString("sourceMsgId");
			ImPrichatMessage sourceMessageParam=new ImPrichatMessage();
			sourceMessageParam.setMsgId(sourceMsgId);
			sourceMessageParam.setMsgId(sourceMsgId);
			ImPrichatMessage sourceMessage = prichatMessageService.selectOneObject(sourceMessageParam);
			if(sourceMessage==null||!sourceMessage.getSendUserid().equals(toUserid)) {
				return;
			}else {
				ImUser sendUserParam=new ImUser();
				sendUserParam.setUserid(sendUserid);
				ImUser sendUser=imUserService.selectOneObject(sendUserParam);
				Map<String,Object> sendUserContact=new HashMap<>();
				if(sendUser!=null){
					if( !StringUtils.hasText(sendUsername)){
						sendUsername=sendUser.getUsername();
					}
					String username=sendUser.getUsername();
					if(!StringUtils.hasText(username)){
						username=sendUsername;
					}
					sendUserContact.put("username",sendUser.getUsername());
					sendUserContact.put("phoneno",sendUser.getPhoneno());
					sendUserContact.put("email",sendUser.getEmail());
					sendUserContact.put("weixin",sendUser.getPhoneno());
				}

				message.put("sendContent",sendUserContact);

				ImUser toUserParam=new ImUser();
				toUserParam.setUserid(sendUserid);
				ImUser toUser=imUserService.selectOneObject(toUserParam);
				Map<String,Object> toUserContact=new HashMap<>();
				if(toUser!=null){
					if( !StringUtils.hasText(toUsername)){
						toUsername=toUser.getUsername();
					}
					String username=toUser.getUsername();
					if( !StringUtils.hasText(username)){
						username=toUsername;
					}
					toUserContact.put("username",username);
					toUserContact.put("phoneno",toUser.getPhoneno());
					toUserContact.put("email",toUser.getEmail());
					toUserContact.put("weixin",toUser.getPhoneno());
				}
				JSONObject askMessage= (JSONObject) message.clone();
				askMessage.put("msgId",seqService.getTablePK("message","id"));
				askMessage.put("sourceMsgId",message.getString("msgId"));
				askMessage.put("action","newMessage");
				askMessage.put("sendObject","C");
				askMessage.put("sendContent",toUserContact);
				askMessage.put("toUserid",sendUserid);
				askMessage.put("toUsername",sendUsername);
				askMessage.put("sendUserid",toUserid);
				askMessage.put("sendUsername",toUsername);
				askMessage.put("msgType","prichat");
				askMessage.put("msgAction",MsgAction.prichat_agreeExchangeContact);
				publisher.publishPrichatMessage(sendUserid,askMessage.toJSONString());
			}
			
		}
	}

	@Override
	public boolean suportMsgAction(String msgAction) {
		return MsgAction.prichat_agreeExchangeContact.equals(msgAction);
	}

}
